//
//  QKEasyClipsAlbumCell.m
//  EasyIOS
//
//  Created by ChiCo on 2017/5/3.
//  Copyright © 2017年 EasyIOS. All rights reserved.
//

#import "QKEasyClipsAlbumCell.h"
#import <Photos/Photos.h>
#import "PHAsset+iCloud.h"

@interface QKEasyClipsAlbumCell ()
@property (nonatomic,strong)UIView *maskView;
@property (nonatomic, strong)UIImageView *photo;
@property (nonatomic, strong)UIImageView *chooseImageView;
@end


@implementation QKEasyClipsAlbumCell
static  CGFloat width;


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        width = KScreenWidth/4 * [UIScreen mainScreen].scale;
        self.photo = ({
            UIImageView *imageView = [[UIImageView alloc] init];
            [self.contentView addSubview:imageView];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.mas_equalTo(0);
            }];
            [imageView setContentMode:UIViewContentModeScaleAspectFill];
            [imageView setClipsToBounds:YES];
            imageView;
        });
        
        self.maskView = ({
            UIView *view = [[UIView alloc] init];
            [self.contentView addSubview:view];
            [view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.mas_equalTo(0);
            }];
            
            UIImageView *imageView = [[UIImageView alloc] init];
            [imageView setImage:[UIImage imageNamed:@"icloud_download"]];
            [view addSubview:imageView];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(23, 18));
                make.right.mas_equalTo(-5);
                make.top.mas_equalTo(5);
            }];
            
            view;
        });
    }
    return self;
}

- (void)setAsset:(PHAsset *)asset
{
    _asset = asset;
    @weakify(self);
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    [options setResizeMode:PHImageRequestOptionsResizeModeExact];
    [options setDeliveryMode:PHImageRequestOptionsDeliveryModeHighQualityFormat];
    [options setNetworkAccessAllowed:YES];
    [self.photo setImage:[UIImage imageNamed:@"album_placeholder"]];
    [[PHImageManager defaultManager] requestImageForAsset:_asset targetSize:CGSizeMake(width, width) contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info)
     {
         @strongify(self);
         if (result) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self.photo setImage:result];
             });
         }
         
     }];
    [self.maskView setHidden:YES];
    [_asset isCloud:^(BOOL iCloud) {
        @strongify(self);
        [self.maskView setHidden:!iCloud];
    }];
}


@end

